package com.speily.web;

import com.speily.web.utils.SocketProcess;
import com.speily.web.utils.UtilsXml;
import org.dom4j.Element;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Auther: SPL
 * @Date: 2019-07-03 11:18
 * @Description:
 */
public class Mytomcat {

    private static final int port = 8099;

    //servlet
    public static final HashMap<String, Object> servlet = new HashMap<>();
    public static final HashMap<String, Object> servletMapping = new HashMap<>();
    //filter
    public static final HashMap<String, Object> filter = new HashMap<>();
    public static final HashMap<String, Object> filterMapping = new HashMap<>();
    //listener
    public static final Set<Object> listener = new HashSet<>();


    private void init() {

        try {
            System.out.println("----------加载配置文件开始");
            //读取web.xml
            UtilsXml xml = new UtilsXml(UtilsXml.class.getResource("/")+"web.xml");
            System.out.println("----------已加载配置文件："+ xml.document.getName());

            //加载 servlet
            List<Element> list = xml.getNodes("servlet");
            for (Element element : list) {
                servlet.put(element.element("servlet-name").getText(), Class.forName(element.element("servlet-class").getText()).newInstance());
            }
            //加载 servlet-mapping
            List<Element> list2 = xml.getNodes("servlet-mapping");
            for (Element element : list2) {
                servletMapping.put(element.element("url-pattern").getText(), element.element("servlet-name").getText());
            }

            //加载 filter
            List<Element> list3 = xml.getNodes("filter");
            for (Element element : list3) {
                filter.put(element.element("filter-name").getText(), Class.forName(element.element("filter-class").getText()).newInstance());
            }
            List<Element> list4 = xml.getNodes("filter-mapping");
            for (Element element : list4) {
                filterMapping.put(element.element("url-pattern").getText(), element.element("filter-name").getText());
            }
            //加载 listener
            List<Element> list5 = xml.getNodes("listener");
            for (Element element : list5) {
                listener.add(Class.forName(element.element("listener-class").getText()).newInstance());
            }
            System.out.println("----------加载配置文件结束");

        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    private void start() {
        try {
            ServerSocket serverSocket = new ServerSocket(port);
            System.out.println("----------Tomcat 服务已启动，地址：localhost ,端口：" + port);
            this.init();
            //持续监听
            do {
                Socket socket = serverSocket.accept();
                System.out.println(socket);
                //处理任务
                Thread thread = new SocketProcess(socket);

                thread.start();
            } while (true);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Mytomcat tomcat = new Mytomcat();
        tomcat.start();

    }

}
